<template>
    <div class="vipCardType">
        <el-dialog title="新建会员卡类型" class="noselect"  :close-on-click-modal="false" :visible.sync="showDialog" :before-close="handleClose">
            <div style="margin: 20px;"></div>
            <el-form :model="modelForm" ref="modelForm" label-width="100px" class="cardForm" >
                <el-form-item label=" " prop="name"  :rules="[{ required: true, message: '名称不能为空', trigger: 'blur' }]">
                <el-input  size='medium' placeholder="请输入卡类型名称;例:钻石卡、金卡、银卡." v-model="modelForm.name" :maxLength="15">
                        <template slot="prepend">
                            <el-tooltip placement="top" content="请填名称">
                                <el-button tabIndex="-1">名称</el-button>
                            </el-tooltip>
                        </template>
                    </el-input>
                    <!--<div class="text-left" style="font-size:13px;color:#ccc">卡类型名称;例:钻石卡、金卡、银卡.</div>-->
                </el-form-item>
                <!--<el-form-item label=" " prop="name">-->
                    <!--<div style="display:flex">-->
                    <!--<div class="el-from-item_title text-left" style="width:120px">微会员</div>-->
                    <!--<div class="el-from-item_content text-left" style="margin-left:20px;width:80px">-->
                        <!--<el-switch-->
                            <!--v-model="modelForm.hasVip"-->
                             <!--on-color="#13ce66"-->
                             <!--off-color="#ff4949"-->
                             <!--on-text="是"-->
                             <!--off-text="否">-->
                        <!--</el-switch>-->
                    <!--</div>-->
                    <!--<div style="font-size:12px;color:#ccc;float:right">开启微会员，该会员类型默认为微信注册会员类型。</div>-->
                    <!--</div>-->
                <!--</el-form-item>-->
              <el-form-item label=" " prop="" :rules="[ { required: true, message: '请选择所属区域', trigger: 'change' }]" style="position:relative">
                <div class="el-input el-input-group el-input-group--prepend el-input--medium" >
                  <div class="el-input-group__prepend" tabindex="-1">适用门店</div>
                  <el-input placeholder="请选择" v-model="districtName" auto-complete="off" :maxlength="5" size="medium" >
                    <el-button slot="append" icon="el-icon-caret-bottom" @click="goChose()"></el-button>
                  </el-input>
                </div>
                <!--style="position: absolute;z-index:999999;background-color: #fff;width:100%;max-height: 30vh;overflow-y: auto;"-->
                <div  v-if="isDistricts" class="branch_tree" >
                  <div style="text-align:left">
                    <el-tree
                      id="myPanel"
                      class="branch_tree_container"
                      :data="treeData"
                      show-checkbox
                      node-key="value"
                      ref="tree"
                      :default-checked-keys="checkedKey"
                      default-expand-all
                      @check="getCurrent">
                    </el-tree>
                  </div>
                </div>
              </el-form-item>
                <el-form-item label=" "  prop="discountId">
                        <div class="el-from-item_title">折扣方案</div>
                        <div class="el-from-item_content text-left" style="margin-left:80px">
                        <el-radio-group class="discount_plan_radio_group" v-model="modelForm.discountId">
                          <el-radio :label="i.id" :value="i.id" v-for="i in planList" :key="i.id" style="margin-bottom: 10px">{{i.name}}</el-radio>
                        </el-radio-group>
                        </div>
                </el-form-item>
                <el-form-item label=" " prop="name">
                    <div style="display:flex">
                    <div class="el-from-item_title text-left" style="width:120px">会员价</div>
                    <div class="el-from-item_content text-left" style="margin-left:20px;width:80px">
                        <el-switch
                            v-model="modelForm.hasVipPrice"
                             on-color="#13ce66"
                             off-color="#ff4949"
                             on-text="是"
                             off-text="否">
                        </el-switch>
                    </div>
                    <div style="font-size:12px;color:#ccc;float:right">开启会员价则使用该类型卡的会员将享受全部会员价菜品</div>
                    </div>
                </el-form-item>
                <el-form-item label=" " prop="name">
                    <div style="display:flex">
                    <div class="el-from-item_title" style="width:120px">启用消费返积分</div>
                    <div class="el-from-item_content text-left" style="margin-left:20px;width:80px">
                        <el-switch
                            v-model="modelForm.hasConsumeRecharge"
                             on-color="#13ce66"
                             off-color="#ff4949"
                             on-text="是"
                             off-text="否">
                        </el-switch>
                    </div>
                    <div style="font-size:12px;color:#ccc;float:right">开启消费返积分功能，该类型卡的会员结账按照下面规则返积分。</div>
                    </div>
                </el-form-item>

              <div v-if="modelForm.hasConsumeRecharge">
                <el-row >
                  <el-col :span="11">
                    <el-form-item style="margin-right:0px" label=" " prop="consumePrice" :rules="[ rules.numPot2,{ required: true, message: '请输入消费金额', trigger: 'blur',type:'number'}]">
                      <el-input placeholder="请输入消费金额" v-model.number="modelForm.consumePrice" @blur="isDouble(modelForm,'consumePrice')" size="medium">
                        <template slot="prepend">
                          <el-button tabIndex="-1">消费金额</el-button>
                        </template>
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="2">
                    <div class="text-center" style="height: 100%;line-height: 35px">返</div>
                  </el-col>
                  <el-col :span="11">
                    <div>
                      <el-form-item style="margin-left: -100px" prop="giftPoint" :rules="[rules.numPot2, { required: true, message: '请输入赠送积分', trigger: 'blur',type:'number'}]">
                        <el-input placeholder="请输入赠送积分" v-model.number="modelForm.giftPoint" @blur="isDouble(modelForm,'giftPoint')" size="medium">
                          <template slot="prepend">
                            <el-button tabIndex="-1">赠送积分</el-button>
                          </template>
                        </el-input>
                      </el-form-item>
                    </div>
                  </el-col>
                </el-row>

                <!--积分设置-->
                <el-form-item label=" " prop="name">
                  <div class="el-from-item_content text-left" style="">
                    <el-input placeholder="请输入积分倍数" v-model="pointRules.birthdayMultiple" @blur="isNum(pointRules,'birthdayMultiple')"  auto-complete="off" :maxlength="5" size="medium" >
                      <template slot="prepend">生日积分</template>
                      <template slot="append">倍</template>
                    </el-input>
                  </div>
                </el-form-item>

                <div class="text-left" style="padding-left: 100px; margin-bottom: 15px">特殊日期积分</div>

                <el-row v-for="(pur,index) in pointRules.specialDate" style="margin-bottom: 20px">
                  <el-form :model="pur" :ref="'pur'+index">
                    <el-col :span="1">&nbsp;
                    </el-col>
                    <el-col :span="2">
                      <div style="height: 100%;line-height: 35px;z-index: 10" class="text-center">
                        <span class="el-icon-plus" style="cursor: pointer;font-size: 15px;padding-right: 5px" @click="addPointRule(index)"></span>
                        <span class="el-icon-minus" style="cursor: pointer;font-size: 15px;padding-left: 5px" @click="minusPointRule(index)"></span>
                      </div>
                    </el-col>
                    <el-col :span="20">
                      <el-row>
                        <el-col :span="14" class="text-left">
                          <el-date-picker size="medium" style="width: 95%;"
                            v-model="pur.dates"
                            type="daterange"
                            range-separator="至" value-format="yyyy-MM-dd"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            :picker-options="pickerOptions"
                            @change="getEntryTime($event,pur,index)"
                          >
                          </el-date-picker>
                        </el-col>
                        <el-col :span="8">
                          <div class="el-from-item_content text-left" style="width:95%;">
                            <el-input placeholder="请输入积分倍数" v-model="pur.multiple" @blur="isNum(pur,'multiple')" auto-complete="off" :maxlength="5" size="medium" >
                              <template slot="append">倍</template>
                            </el-input>
                          </div>
                        </el-col>
                        <el-col :span="2">

                        </el-col>
                      </el-row>
                    </el-col>

                  </el-form>
                </el-row>

                <el-form-item label=" " prop="name">
                  <div class="el-input el-input-group el-input-group--prepend el-input--medium">
                    <div class="el-input-group__prepend" tabindex="-1">重叠后优先使用</div>
                    <el-select v-model="pointRules.useType" placeholder="请选择" class="w-100p">
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </div>
                </el-form-item>

                <!--积分设置结束-->
              </div>

                <el-form-item label=" " prop="name">
                    <div style="display:flex">
                    <div class="el-from-item_title text-left" style="width:120px">启用充值规则</div>
                    <div class="el-from-item_content text-left" style="margin-left:20px;width:80px">
                        <el-switch
                            v-model="modelForm.rechargeRule"
                             on-color="#13ce66"
                             off-color="#ff4949"
                             on-text="是"
                             off-text="否">
                        </el-switch>
                    </div>
                    <div style="font-size:12px;color:#ccc;float:right">开启充值规则功能，该类型会员卡在充值时按照以下规则返还。</div>
                    </div>
                </el-form-item>
                <el-form-item label=" " prop="powerModel" v-if="modelForm.rechargeRule">
                    <div class="el-input el-input-group el-input-group--prepend el-input--medium" >
                        <div class="el-input-group__prepend" tabindex="-1">充值规则</div>
                        <el-select class="el-cascader-margin w-100p" v-model="modelForm.rechargeType" placeholder="请选择充值规则">
                            <el-option
                            v-for="item in rechargeTypes"
                            :key="item.key"
                            :label="item.text"
                            size="medium"
                            :value="item.key">
                            </el-option>
                        </el-select>
                    </div>
                </el-form-item>
                <el-row v-for="(pur,index) in topupRules" v-if="modelForm.rechargeRule && modelForm.rechargeType!='ADDCASHBACK'"  style="margin-bottom: 5px">
                  <el-form :model="pur" :ref="'pur'+index"  class="demo-ruleForm">
                    <el-col v-if="modelForm.rechargeType=='FIXEDCASHBACK'" :span="2">
                    </el-col>
                    <el-col :span="3">
                        <div style="height: 100%;line-height: 35px;z-index: 10" class="text-center">
                            <span class="el-icon-plus" style="cursor: pointer;font-size: 15px;padding-right: 5px" @click="addColumn(index)"></span>
                            <span class="el-icon-minus" style="cursor: pointer;font-size: 15px;padding-left: 5px" @click="minusColumn(index)"></span>
                        </div>
                    </el-col>
                    <el-col :span="4"><!--:prop="'pur.'+index+'.rangeStart'"-->
                        <el-form-item style="margin-left:0px;margin-right:0" class="clearFormMargin" prop="rangeStart" :rules="[{
                             required: true, message: '必填信息', trigger: 'blur',type:'number'
                        },rules.numPot2]">
                            <el-input placeholder="100" @blur="isDouble(pur,'rangeStart')" v-model.number="pur.rangeStart" size="medium">
                                <el-button slot="append">元</el-button>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="1" v-if="modelForm.rechargeType!='FIXEDCASHBACK' && modelForm.rechargeType!=''"><div style="height: 100%;line-height: 35px" class="text-center">至</div></el-col>
                    <el-col :span="4" v-if="modelForm.rechargeType!='FIXEDCASHBACK' && modelForm.rechargeType!=''">
                        <el-form-item style="margin-left:0px;margin-right:0" class="clearFormMargin" prop="rangeEnd" :rules="[{
                             required: true, message: '必填信息', trigger: 'blur',type:'number'
                        },rules.numPot2]">
                            <el-input placeholder="1000" @blur="isDouble(pur,'rangeEnd')" v-model.number="pur.rangeEnd" size="medium">
                                <el-button slot="append">元</el-button>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2"><div style="height: 100%;line-height: 35px" class="text-center">返现</div></el-col>
                    <el-col :span="4">
                        <el-form-item style="margin-left:0px;margin-right:0" class="clearFormMargin" prop="giftMoney" :rules="[{
                             required: true, message: '必填信息', trigger: 'blur',type:'number'
                        },rules.numPot2]">
                            <el-input placeholder="10" @blur="isDouble(pur,'giftMoney')" v-model.number="pur.giftMoney" size="medium">
                                <el-button slot="append">元</el-button>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <div>
                        <el-col :span="1">
                            <div style="height: 100%;line-height: 35px;font-size: 18px" class="text-center">
                            +
                            </div>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item style="margin-left:0px;margin-right:0" class="clearFormMargin" prop="giftPoint" :rules="[{
                             required: true, message: '必填信息', trigger: 'blur',type:'number'
                             },rules.numPot2]">
                                <el-input placeholder="100" @blur="isDouble(pur,'giftPoint')" v-model.number="pur.giftPoint" size="medium">
                                    <el-button slot="append">积分</el-button>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </div>
                  </el-form>
                </el-row>

              <el-row v-if="modelForm.rechargeRule && modelForm.rechargeType=='ADDCASHBACK'" style="padding-right: 100px; padding-left: 100px;">
                <el-col :span="12">
                  <el-input placeholder="请输入充值金额" v-model="rechargeAmount" class="input-with-select">
                    <template slot="prepend" >
                       充值
                    </template>
                    <el-button slot="append">元</el-button>
                  </el-input>
                </el-col>
                <el-col :span="12">
                  <el-input placeholder="请输入赠送金额" v-model="rechargeGiftAmount" class="input-with-select">
                    <template slot="prepend" >
                       赠送
                    </template>
                    <el-button slot="append">元</el-button>
                  </el-input>
                </el-col>
              </el-row>

              <!--使用规则 start-->
              <el-form-item label=" " prop="name">
                <div style="display:flex">
                  <div class="el-from-item_title text-left" style="width:120px">启用使用规则</div>
                  <div class="el-from-item_content text-left" style="margin-left:20px;width:80px">
                    <el-switch
                      v-model="modelForm.hasUseRule"
                      on-color="#13ce66"
                      off-color="#ff4949"
                      on-text="是"
                      off-text="否">
                    </el-switch>
                  </div>
                  <div style="font-size:12px;color:#ccc;float:right">开启启用使用规则功能，该类型会员卡在使用时按照以下规则使用。</div>
                </div>
              </el-form-item>
              <!--使用规则 end-->
              <div v-if="modelForm.hasUseRule">
                <el-form-item label=" " prop="">
                  <div class="el-input el-input-group el-input-group--prepend el-input--medium">
                    <div class="el-input-group__prepend" tabindex="-1">使用类型</div>
                    <el-select v-model="useType" placeholder="请选择" class="w-100p" @change="changeUseType">
                      <el-option
                        v-for="item in useTypes"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </div>
                </el-form-item>



                <el-form-item v-if="useType=='everyDay'" label=" " prop="value" >
                  <el-input  size='medium' placeholder="请输入每天限额多少元"  @blur="isDouble(useRuleDetail,'value')" v-model="useRuleDetail.value" :maxLength="10">
                    <template slot="prepend">
                        <el-button tabIndex="-1">按天限额(固定金额)</el-button>
                    </template>
                    <template slot="append">
                        <el-button tabIndex="-1">元</el-button>
                    </template>
                  </el-input>
                </el-form-item>

                  <el-form-item v-if="useType=='everyTime'" label=" " prop="value" >
                    <el-input  size='medium' placeholder="请输入每天限额多少元"  @blur="isDouble(useRuleDetail,'value')" v-model="useRuleDetail.value" :maxLength="10">
                      <template slot="prepend">
                        <el-button tabIndex="-1">按单限额(固定金额)</el-button>
                      </template>
                      <template slot="append">
                        <el-button tabIndex="-1">元</el-button>
                      </template>
                    </el-input>
                  </el-form-item>

                    <el-form-item v-if="useType=='everyTimeRatio'" label=" " prop="value" >
                      <el-input  size='medium' placeholder="请输入每天限额多少元"  @blur="isDouble(useRuleDetail,'value')" v-model="useRuleDetail.value" :maxLength="2">
                        <template slot="prepend">
                          <el-button tabIndex="-1">按单限额(金额比例)</el-button>
                        </template>
                        <template slot="append">
                          <el-button tabIndex="-1">%</el-button>
                        </template>
                      </el-input>
                    </el-form-item>
              </div>

            </el-form>
                        <div class="foot-bar-button">
                          <el-button :disabled="canSave" type="primary" size="medium" @click="submitForm('modelForm')">
                            <span v-if="canSave"><i class="el-icon-loading"></i>提交中</span>
                            <span v-else>提交</span>
                          </el-button>
                            <!--<el-button type="primary" size="medium" @click="submitForm('modelForm')">提交</el-button>-->
                            <el-button @click="resetForm('modelForm')"  size="medium">取消</el-button>
                        </div>
        </el-dialog>
    </div>
</template>
<script>
//注意路径

export default {
  components: {
  },
  props: ["isShow", "id"], //参数
  data() {
    return {
      canSave:false,
      modelForm: {
      },
      isDistricts:false,
      hasVipPrice: false,
      districtName:"",
      treeData:[],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      checkedKey:[],
      showDialog: false,
      activityId: "",
      districtId:[],
      isSave: true,
      states: [],
      value2: true,
      planList:[],
      rechargeTypes:[],
      topupRules: {
      rangeStart: "",
      rangeEnd: "",
      giftMoney: "",
      giftPoint: "",
        rechargeAmount:0.0,
        rechargeGiftAmount:0.0
    },
      options: [{
        value: 'birthday',
        label: '会员生日'
      }, {
        value: 'special',
        label: '特殊日期'
      }, {
        value: 'superimposed',
        label: '叠加'
      }],
      pointRules:{
        birthdayMultiple:1, //生日积分几倍
        specialDate:[{ //特殊日期
          type:'date',
          start:'',
          end:'',
          multiple:1
        }],
        useType:'special' //重叠优先使用类型
      },//积分规则
      pickerOptions:{
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      useRuleDetail:{
        type:"",//everyDay/everyTime/everyTimeRatio(每天限额xx元/每次限额xx元/每次限额比例xx)
        value:""

      },
      useTypes:[{label:'按天限额(固定金额)',value:'everyDay'},{label:'按单限额(固定金额)',value:'everyTime'},{label:'按单限额(金额比例)',value:'everyTimeRatio'}],
      useType:"everyDay"
    };
  },
  watch: {
    isShow: function(newValue, oldValue) {
      this.showDialog = newValue;
      this.canSave=false;
      if (newValue) {
        this.districtName = "";
        this.checkedKey = [];
        if (this.id != "") {
          this.activityId = this.id;
          this.isSave = false;
          this.$axios
            .get("members/memberCardTypes/" + this.activityId + "/update")
            .then(data => {
              if(!data.content.memberCardType.discountId){
                data.content.memberCardType.discountId='';
              }
              this.modelForm = data.content.memberCardType;
              var tree = data.content.districts;
              this.newLabel(tree);
              this.treeData = tree;
              data.content.planList.splice(0,0,{id:"",name:"无折扣"})
              this.planList=data.content.planList;
              this.rechargeTypes=data.content.rechargeTypes;
              if( this.modelForm.rechargeRule && this.modelForm.rechargeType!='ADDCASHBACK'){
                this.topupRules=data.content.ruleList;
              }else if(this.modelForm.rechargeRule && this.modelForm.rechargeType=='ADDCASHBACK') {
                this.topupRules=[
                  {
                    rangeStart: "",
                    rangeEnd: "",
                    giftMoney: "",
                    giftPoint: ""
                  }
                ];
                if(data.content.ruleList && data.content.ruleList.length){
                  this.rechargeAmount = data.content.ruleList[0].rangeStart;
                  this.rechargeGiftAmount = data.content.ruleList[0].giftMoney;
                }
              }else {
                this.topupRules=[
                  {
                    rangeStart: "",
                    rangeEnd: "",
                    giftMoney: "",
                    giftPoint: ""
                  }
                ];
              }
              var branchs=[];
              for(var i=0;i<data.content.branchList.length;i++){
                branchs.push(data.content.branchList[i].branchId);
              }
             var selectDistrict=[];
              this.districtId=[];
              for(let i=0;i<data.content.branchList.length;i++){
                selectDistrict.push({name:data.content.branchList[i].label,id:data.content.branchList[i].branchId,districtId:data.content.branchList[i].districtId});
                this.districtId.push(data.content.branchList[i].branchId);
              }
              this.districtName="";
              if(selectDistrict.length>0){
                for(var j=0;j< selectDistrict.length;j++){
                  this.districtName +=selectDistrict[j].name+",";
                }
              }
              var ids=[];
              for(var i=0;i<selectDistrict.length;i++){
                ids.push(selectDistrict[i].id+"_b");
              }
              this.checkedKey=ids;

              //积分规则
              if(data.content.memberCardType.pointRule){
                var pointRule = JSON.parse(data.content.memberCardType.pointRule);
                this.pointRules = pointRule;
              }

            });
        } else {
          this.isSave = true;
          this.$axios.get("members/memberCardTypes/create").then(data => {
            data.content.memberCardType.discountId='';
            this.modelForm = data.content.memberCardType;
            this.topupRules=[
              {
                rangeStart: "",
                rangeEnd: "",
                giftMoney: "",
                giftPoint: ""
              }
            ];

            var tree = data.content.districts;
            this.newLabel(tree);
            this.treeData = tree;
            data.content.planList.splice(0,0,{id:"",name:"无折扣"})
            this.planList=data.content.planList;
            this.rechargeTypes=data.content.rechargeTypes;
            this.states = data.content.states;
          });
        }
      }
    }
  },
  mounted(){
    let _this=this;
    document.addEventListener("click",function(event){
      var sp = document.getElementById("myPanel");
      if(sp){
        if(!sp.contains(event.target)){            //这句是说如果我们点击到了id为myPanel以外的区域
          _this.isDistricts=false;
        }
      }
    })
  },
  created() {
    window.this = this;
  },
  methods: {
    changeUseType(){
      this.useRuleDetail.value = "";
    },
    getEntryTime(e,pur,index){
      if(e && e.length==2){
        pur.start = e[0];
        pur.end = e[1];

        var start =  new Date(pur.start+" 00:00:00");
        var end = new Date(pur.end+" 23:59:59");


        var st ;
        var ed ;
        if(this.pointRules.specialDate && this.pointRules.specialDate.length>0){
          for(var i=0;i<this.pointRules.specialDate.length;i++){
            st="" ;
            ed="" ;
            //当前时间和其他选择的特殊日期比较 重叠提示日期重叠
            if(i!=index){
               st = new Date(this.pointRules.specialDate[i].start+" 00:00:00");
               ed = new Date(this.pointRules.specialDate[i].end+" 23:59:59");
               if((start.getTime()>=st.getTime() && start.getTime()<=ed.getTime()) || (end.getTime()>=st.getTime() && end.getTime() <=ed.getTime())){
                 this.$message({
                   message: '当前所选日期与其他特殊日期有重叠，请重新选择',
                   type: 'warning'
                 });
                 this.pointRules.specialDate[index].start="";
                 this.pointRules.specialDate[index].end="";
                 this.pointRules.specialDate[index].dates=[];
                 break;
               }
            }

          }
        }

      }
    },
    addPointRule(index) {
      this.pointRules.specialDate.splice(index + 1, 0, {
        type:'date',
        start:'',
        end:'',
        multiple:1
      });
    },
    minusPointRule(index) {
      if (this.pointRules.specialDate.length > 1) {
        this.pointRules.specialDate.splice(index, 1);
      }
    },


    goChose(){
      this.isDistricts=!this.isDistricts;
    },
    getCurrent(){
      this.selectDistrict=[];
      this.districtId=[];
      this.checkedKey=[];
      var getCheck=this.$refs.tree.getCheckedNodes();
      for(let i=0;i<getCheck.length;i++){
        if(getCheck[i].iBranch==true){
          this.selectDistrict.push({name:getCheck[i].name,id:getCheck[i].id});
          this.districtId.push(getCheck[i].id);
        }
      }
      this.districtName="";
      if(this.selectDistrict.length>0){
        for(var j=0;j< this.selectDistrict.length;j++){
          this.checkedKey.push(this.selectDistrict[j].id+"_b")
          this.districtName +=this.selectDistrict[j].name+",";
        }
      }
    },
    newLabel(newList) {
      if (newList) {
        for (var i = 0; i < newList.length; i++) {
          if(newList[i].iBranch){
            newList[i].value = newList[i].id + "_b";
          }else {
            newList[i].value = newList[i].id + "";
          }
          newList[i].label = newList[i].name;
          if (!newList[i].children || newList[i].children.length == 0) {
            newList[i].children = null;
          } else {
            for (var s = 0; s < newList[i].children.length; s++) {
              if(newList[i].iBranch){
                newList[i].children[s].value = newList[i].children[s].id + "_b";
              }else {
                newList[i].children[s].value = newList[i].children[s].id + "";
              }
              newList[i].children[s].label = newList[i].children[s].name;
              this.newLabel(newList[i].children[s].children);
            }
          }
        }
      }
    },

    addColumn(index) {
      this.topupRules.splice(index + 1, 0, {
        rangeStart: "",
        rangeEnd: "",
        giftMoney: "",
        giftPoint: ""
      });
    },
    minusColumn(index) {
      if (this.topupRules.length > 1) {
        this.topupRules.splice(index, 1);
      }
    },
    goBack() {
      this.router.go(-1);
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var data={};
          data.types=this.modelForm;
          data.branchs=JSON.stringify(this.districtId);
          if(this.modelForm.rechargeRule && this.modelForm.rechargeType!='ADDCASHBACK'){
            data.rules=JSON.stringify(this.topupRules);
          }else if(this.modelForm.rechargeRule && this.modelForm.rechargeType=='ADDCASHBACK'){
            this.topupRules = [];
            this.topupRules.push({
              rangeStart:this.rechargeAmount,
              giftMoney:this.rechargeGiftAmount
            });
            data.rules = JSON.stringify(this.topupRules);
          }

          //积分规则
          var specialDate = [];
          if(this.pointRules && this.pointRules.specialDate && this.pointRules.specialDate.length){
            for(var i=0;i<this.pointRules.specialDate.length;i++){
              if(this.pointRules.specialDate[i].start!="" && this.pointRules.specialDate[i].end!=""){
                specialDate.push(this.pointRules.specialDate[i]);
              }
            }
            this.pointRules.specialDate = specialDate;
          }
          data.pointRule=JSON.stringify(this.pointRules);
          //使用规则
          if(this.modelForm.hasUseRule){
            this.useRuleDetail = {
              value:this.useRuleDetail.value,
              type:this.useType
            }

            data.types.useRuleDetail=JSON.stringify(this.useRuleDetail);
          }



//          return ;

          if(this.canSave){
            return;
          }
          this.canSave=true;
          if (this.isSave) {
            this.$axios
              .post("members/memberCardTypes/create", data,"json")
              .then(data => {
                this.canSave=false;
                if (data.status == 200) {
                  this.$message({
                    message: data.message,
                    type: "success"
                  });
                  this.$emit("addSuccess");
                } else {
                  this.$message.error(data.message);
                }
              });
          } else {
            this.modelForm._method = "PUT";
            this.$axios
              .put(
                "members/memberCardTypes/" + this.activityId + "/update",
               data,"json"
              )
              .then(data => {
                if (data.status == 200) {
                  this.$message({
                    message: data.message,
                    type: "success"
                  });
                  this.$emit("addSuccess");
                } else {
                  this.$message.error(data.message);
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$emit("close");
      this.showDialog = false;
    },
    handleClose(done) {
      this.$emit("close");
      done();
    },
    isNum(item, key) {
      var r = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
      if (!r.test(item[key])) {
        item[key] = "1";
      } else {
        item[key] = item[key];
      }
    },
    isDouble(item, key) {
      var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
      if (!isNum.test(item[key])) {
        item[key] = "0.00";
      } else {
        item[key] = item[key];
      }
    }
  }
};
</script>
<style scoped>
.el-from-item_title {
  text-align: left;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #48576a;
  line-height: 1;
  padding: 11px 12px 11px 0;
  box-sizing: border-box;
}
.el-from-item_content {
  line-height: 36px;
  position: relative;
  font-size: 15px;
}
  .discount_plan_radio_group{
    /*background-color: #00bcd4;*/
    padding-left: 30px;
    padding-top: 5px;
  }
  .discount_plan_radio_group .el-radio{
    margin-left: 0px;
    width:15vh;
    height: 30px;
    /*background-color: #8bc34a;*/
    line-height: 30px;

  }
</style>
